package top.codedance.iotp.iot.sdk;

import top.codedance.iotp.iot.sdk.util.StompTool;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.net.URISyntaxException;

public class StompWsContext extends WsContextAdapter {
    private WsCallbacker callbacker;
    private String topic;
    private String id;
    public StompWsContext(String url, String topic, String id, WsCallbacker callbacker) throws URISyntaxException {
        super(new URI(url));
        this.topic = topic;
        this.id = id;
        this.callbacker = callbacker;
    }

    @Override
    public void onOpen(ServerHandshake shake) {
        StompTool stompTool = new StompTool(this);
        stompTool.connect();
        stompTool.subscribe(id, topic);
        callbacker.onOpen(this, shake);
    }

    @Override
    public void onMessage(String body) {
        callbacker.onMessage(this, body);
    }

    @Override
    public void onClose(int paramInt, String paramString, boolean paramBoolean) {
        callbacker.onClose(this, paramInt, paramString, paramBoolean);
    }

    @Override
    public void onError(Exception e) {
        callbacker.onError(this, e);
    }
}
